#include "StompedReality/Asset/Windows/WGLFontManager.h"
#include "StompedReality/Core/Graphics/Windows/WGLRenderSystem.h"
#include "StompedReality/Core/IGame.h"
#include "StompedReality/Core/Platform.h"
#include <assert.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <windows.h>
namespace StompedReality
{
namespace Graphics
{
WGLFontManager::WGLFontManager( ) :
  FontManager( )
{
}

WGLFontManager::~WGLFontManager( )
{
}

HFONT WGLFontManager::_createHFONT( const char* font, int size ) {
  return CreateFont( -size,        // Height Of Font
                     0, // Width Of Font
                     0, // Angle Of Escapement
                     0, // Orientation Angle
                     FW_BLACK, // Font Weight
                     false, // Italic
                     false, // Underline
                     false, // Strikeout
                     ANSI_CHARSET, // Character Set Identifier
                     OUT_TT_PRECIS, // Output Precision
                     CLIP_DEFAULT_PRECIS, // Clipping Precision
                     ANTIALIASED_QUALITY, // Output Quality
                     FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
                     font ); // Font Name
}

int WGLFontManager::loadFont( const char* font, int size ) {
  assert( this->m_Game != 0 );
  HDC hdc = ( ( StompedReality::Graphics::WGLRenderSystem* ) this->m_Game->RenderSystem( ) )->GetHDC( );
  HFONT hFont = this->_createHFONT( font, size );
  int list = glGenLists( 255 );
  HFONT oldfont = (HFONT)SelectObject( hdc, hFont );
  wglUseFontBitmaps ( hdc, 0, 255, list );
  SelectObject( hdc, oldfont );
  DeleteObject( hFont );
  return list;
}

void WGLFontManager::selectFont( StompedReality::ID id, int size ) {
  int list = this->getFont( id, size );
  if (list == -1) {
    glListBase( this->_defaultFont );
    return;
  }
  glListBase( list );
}

StompedReality::Math::Vector2 WGLFontManager::calculateSize( const char* font, int size, const char* txt ) {
  HDC hdc = ( ( StompedReality::Graphics::WGLRenderSystem* ) this->m_Game->RenderSystem( ) )->GetHDC( );
  HFONT hFont = this->_createHFONT( font, size );
  HFONT oldfont = (HFONT)SelectObject( hdc, hFont );
  SIZE rect;
  const char* p = strchr( txt, '\n' );
  StompedReality::Math::Vector2 res( 0, 0 );
  if ( p == 0) {
    if (strlen( txt ) == 0)
      GetTextExtentPoint32( hdc, " ", 1, &rect );
    else
      GetTextExtentPoint32( hdc, txt, strlen( txt ) + 1, &rect );
    res.SetX((float)rect.cx + 2 );
    res.SetY((float)rect.cy );
    SelectObject( hdc, oldfont );
    DeleteObject( hFont );
    return res;
  }
  const char* lp = txt;
  while (p != 0) {
    int len = (p - lp);
    if (len == 0)
      GetTextExtentPoint32( hdc, " ", 1, &rect );
    else
      GetTextExtentPoint32( hdc, p, len, &rect );
    res.SetX( MAX( res.GetX( ), (float)rect.cx ));
    res.SetY( res.GetY( ) + rect.cy );
    lp = p + 1;
    p = strchr( p + 1, '\n' );
  }
  if (strlen( lp ) == 0)
    GetTextExtentPoint32( hdc, " ", 1, &rect );
  else
    GetTextExtentPoint32( hdc, lp, strlen( lp ), &rect );
  res.SetX( MAX( res.GetX( ), (float)rect.cx ));
  res.SetY( res.GetY( ) + rect.cy );
  SelectObject( hdc, oldfont );
  DeleteObject( hFont );
  return res;
}

}
}